import router from '@/router'

let userInfo = localStorage.getItem('r-ad-userinfo')
if (userInfo) {
  userInfo = JSON.parse(userInfo)
}

const state = {
  userInfo: userInfo || {},
  tagList: [
    {
      path: 'login',
      meta: { title: 'login' }
    }
  ],
  navigation: [],
  /* 设计工作 */
  designWorks: [],
  extraWorks: [],
  allWorks: [],
  /* 工作台 */
  unfinishWorks: [],
  /* 成员列表 */
  members: [],
  jiantou: true
}

const mutations = {
  //登录
  SET_USER_INFO(state, data) {
    state.userInfo = data
  },
  //退出登录
  LOG_OUT(state) {
    state.userInfo = {}
  },
  //添加导航
  ADD_NAVIGATION({ navigation }, nav) {
    navigation.push(nav)
  },
  ADD_TAG({ tagList }, route) {
    tagList.push(route)
  },
  CLOSE_TAG({ tagList }, index) {
    tagList.splice(index, 1)
  },
  CLOSE_TAG_LEFT({ tagList }, index) {
    tagList.splice(0, index)
  },
  CLOSE_TAG_RIGHT({ tagList }, index) {
    tagList.splice(index + 1, tagList.length - index + 1)
  },
  CLOSE_TAG_ALL(state) {
    state.tagList = []
  },
  ADD_DESIGN_WORKS(state, data) {
    state.designWorks = data
  },
  ADD_EXTRA_WORKS(state, data) {
    state.extraWorks = data
  },
  ADD_ALL_WORKS(state, data) {
    state.allWorks = data
  },
  ADD_UNFINISH_WORKS(state, data) {
    state.unfinishWorks = data
  },
  ADD_MEMBERS(state, data) {
    state.members = data
  },
  SET_IS_EXTEND(state, jiantou) {
    state.jiantou = jiantou
  }
}

const actions = {
  addTag({ commit }, route) {
    commit('ADD_TAG', route)
  },
  closeTag({ commit }, index) {
    commit('CLOSE_TAG', index)
  },
  closeTag_left({ commit }, index) {
    commit('CLOSE_TAG_LEFT', index)
  },
  closeTag_right({ commit }, index) {
    commit('CLOSE_TAG_RIGHT', index)
  },
  closeTag_all({ commit }) {
    commit('CLOSE_TAG_ALL')
  },
  /* 退出登录 */
  logout({ commit }) {
    for (let name of Object.keys(window.localStorage.valueOf())) {
      if (name != '$adminAccountPassword') {
        window.localStorage.removeItem(name)
      }
    }
    window.localStorage.clear()
    commit('LOG_OUT')
    window.sessionStorage.clear()
    router.push('/login')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
